from selenium import webdriver
import selenium
import time
import os
import re
from wordcloud import wordcloud,ImageColorGenerator
import imageio
import jieba
options = webdriver.ChromeOptions()
prefs = {
    'profile.default_content_setting_values':
        {
            'notifications': 2
        }
}
options.add_experimental_option('prefs', prefs)
wb= webdriver.Chrome(chrome_options=options)
time.sleep(5)


def main():
    url=input("输入微博文章的网址：")
    AskUrl(url)
    nums=int(input("需要多少页评论?"))
    datalist=getData(nums)
    make_wordcloud(datalist)
def AskUrl(baseurl):
    wb.get(baseurl)
    wb.implicitly_wait(5)
#获取数据
def getData(nums):
    datalist=[]
    #将页面拉到底部
    js = "var q=document.documentElement.scrollTop=1000000"
    wb.execute_script(js)
    time.sleep(3)
    for i in range(nums):
        print(i)
        accept=False
        while not accept:
            try:
                wb.execute_script(js)
                time.sleep(2)
                wb.find_elements_by_xpath('//span[@class="more_txt"]')[0].click()
                accept=True
            except selenium.common.exceptions.ElementNotInteractableException:
                wb.execute_script(js)
                time.sleep(2)
            except IndexError:
                wb.execute_script(js)
                time.sleep(2)
            except selenium.common.exceptions.StaleElementReferenceException:
                wb.execute_script(js)
                time.sleep(2)
    texts=wb.find_elements_by_xpath('//div[@class="WB_text"]')
    print("一共爬取%d条评论"%len(texts))
    for i in range(len(texts)):
        t=texts[i].text
        name=re.compile('(.*?)：')
        tt=t.replace(" ¡评论配图",'')
        tt=name.sub('',tt)
        if "回复" in tt:
            pass
        else:
            datalist.append(tt)
            print(tt)
    return datalist
#制作词云
def make_wordcloud(datalist):
    img=imageio.imread(r'1.png')
    w=wordcloud.WordCloud(width=1000,height=1000,background_color="white",font_path="msyh.ttc",max_words=400,mask=img,stopwords={"了","啊","还","也","呢","的","是","我","真的","很","有","都","能","不","在","才"})
    data=' '.join(datalist)
    datas=jieba.lcut(data)
    datas_end=[]
    for i in range(len(datas)):
        if datas[i]=="爱" or len(datas[i])!=1:
            datas_end.append(datas[i])

    data_end=' '.join(datas_end)
    w.generate(data_end)

    # image_color=ImageColorGenerator(img)
    # wc=w.recolor(color_func=image_color)

    name=input("输入图片名：")
    if not os.path.exists("D:\\词云图片"):
        os.makedirs("D:\\词云图片")
    w.to_file("D:\\词云图片\\%s.jpg"%name)
    # wc.to_file("D:\\词云图片\\%s.jpg" % name)

if __name__ == '__main__':
    main()
